const { Sequelize,DataTypes  } = require('@sequelize/core');
const sequelize = new Sequelize('ddxt', 'root', '123456', {
  host: '127.0.0.1',
  dialect:'mariadb'
});
const ddxt = sequelize.define("order", 
{
	id: {
		type:DataTypes.INTEGER,
		primaryKey: true
	},
	menu:DataTypes.TEXT,
	code: DataTypes.INTEGER,
	price: DataTypes.FLOAT
},
	{ 
		timestamps: false ,
		
		freezeTableName: true,
	}
);

async function updateOrder(id,menu,code,price){
	if(arguments.length<4){
		return {
			code:400,
			msg:"更新数据失败",
			errMsg:"参数长度不正确"
		}
	}else{
		let [state]=await ddxt.update({menu,code,price},{where: {
			id:id
		}})
		return {
			code:0,
			msg:"已更新",
			errMsg:!!state
		}
	}
}
async function getOrder(code){
	if(isNaN(code)){
		return {
			code:400,
			msg:"查询失败",
			errMsg:"code不是整数"
		}
	}
	let [{dataValues:order}]=await ddxt.findAll({
		where: {
			code
		}
	})
	return {
		code:0,
		msg:"查询成功",
		data:order
	}
}
// getOrder(3).then(res=>{
// 	console.log("res:",res);
// })
async function newData(code){
	try{
		return {
			code:0,
			msg:"插入成功",
			errMsg:await ddxt.create({menu:"{}",code,price:0})
		}
	}catch(err){
		return {
			code:400,
			msg:"插入的code可能重复",
			errMsg:err
		}
	}
}
// newData(10).then(res=>{
// 	console.log("res:",res);
// })
module.exports={
	updateOrder,
	getOrder,
	newData
}